<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top: 5%; height: 85%; overflow-y: hidden"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model" :rules="validatorRules">
        <a-form-model-item label="用户名称">
          <a-input v-model="model.name" :disabled="true" />
        </a-form-model-item>
        <a-form-model-item label="角色" required prop="selectedRoleIds">
          <a-select
            v-model="model.selectedRoleIds"
            mode="multiple"
            style="width: 100%"
            placeholder="请选择角色"
            @change="handleChange"
          >
            <a-select-option v-for="role in roleList" :key="role.id">
              {{ role.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { allocateUserRoles, getUserRole } from '@/api/system'
export default {
  name: 'UserRoleModal',
  data () {
    return {
      title: '分配角色',
      visible: false,
      model: {},
      layout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      selectedRoleIds: [],
      roleList: [],
      validatorRules: {
        selectedRoleIds: [{ required: true, message: '请选择要分配的角色!' }]
      }
    }
  },
  created () {},
  methods: {
    getUserRole () {
      getUserRole(this.model.id).then((res) => {
        if (res.data) {
          res.data.forEach((role) => {
            this.model.selectedRoleIds.push(role.id)
          })
        }
      })
    },
    show (record) {
      this.model = Object.assign({ selectedRoleIds: [] }, record)
      this.getUserRole()
      // 用户拥有的角色
      this.roleList = this.$store.getters.roles
      this.visible = true
    },
    close () {
      const that = this
      that.$emit('close')
      that.visible = false
      that.$refs.form.resetFields()
      that.selectedRoleIds = []
      that.roleList = []
    },
    handleChange (value) {
      console.log(value)
    },
    handleOk () {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          const param = {
            userId: that.model.id,
            roleIdList: this.model.selectedRoleIds
          }
          allocateUserRoles(param)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel () {
      this.close()
    }
  }
}
</script>

<style scoped>
</style>
